package com.snow.campuslink.user.controller;

import com.snow.campuslink.annotation.RequestLog;
import com.snow.campuslink.project.BaseRestController;
import com.snow.campuslink.project.constant.Enum.ModuleEnum;
import com.snow.campuslink.project.result.Result;
import com.snow.campuslink.user.converter.dto.AddUserDto;
import com.snow.campuslink.user.converter.dto.UserLoginDto;
import com.snow.campuslink.user.converter.dto.UserRegisterDto;
import com.snow.campuslink.user.entity.User;
import com.snow.campuslink.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.hutool.core.bean.BeanUtil.copyProperties;
import static com.snow.campuslink.utils.ParamCheckUtil.checkValidResult;


/**
 *  前端控制器
 *
 * @author Gaosl
 * @since 2021-01-20
 */
@RestController
@RequestMapping("/user/user")
public class UserController extends BaseRestController<IUserService,User> {

    @Resource
    private IUserService userService;

    @ApiOperation(value = "用户登录:根据手机号phoneNumber/用户名username/email邮箱地址+密码password,返回user对象")
    @RequestLog(module = ModuleEnum.USER_LOGIN,description = "用户登录")
    @PostMapping("login")
    public Result login(@Validated @RequestBody UserLoginDto userLoginDto, BindingResult result){
        checkValidResult(result);
        User loginUser = userService.login(userLoginDto);
        return Result.success().data(loginUser);
    }

    @ApiOperation(value = "用户注册")
    @RequestLog(module = ModuleEnum.USER_LOGIN,description = "用户注册")
    @PostMapping("register")
    public Result register(@Validated @RequestBody UserRegisterDto userRegisterDto, BindingResult result){
        checkValidResult(result);
        userService.register(userRegisterDto);
        return Result.success();
    }


    @ApiOperation(value = "验证码验证")
    @RequestLog(module = ModuleEnum.USER_LOGIN,description = "用户注册")
    @GetMapping("verificationCode/{email}")
    public Result verificationCode(@PathVariable(value = "email") String email){
        userService.verificationCode(email);
        return Result.success();
    }

    @ApiOperation(value = "个人主页:根据用户userId查询用户个人信息，返回user对象")
    @RequestLog(module = ModuleEnum.USER_LOGIN,description = "个人主页")
    @GetMapping("getUserInfo/{userId}")
    public Result getUserInfoById (@PathVariable(value = "userId") Long userId){
        User userInfo = userService.getUserInfoById(userId);
        return Result.success().data(userInfo);
    }

    @ApiOperation(value = "添加/修改个人详细信息")
    @RequestLog(module = ModuleEnum.USER_LOGIN,description = "添加个人详细信息")
    @PutMapping("addUserInfo")
    public Result addUserInfo(@Validated @RequestBody AddUserDto addUserDto, BindingResult result){
        checkValidResult(result);
        User user = new User();
        copyProperties(addUserDto, user, false);
        userService.updateById(user);
        return Result.success();
    }

    @ApiOperation(value = "Feign接口")
    @GetMapping("getUsernameById/{userId}")
    public String getUsernameById(@PathVariable Long userId){
        return userService.getUsernameById(userId);
    }



}

